<!--邀请奖励配置-->
<template>
  <div class="app-container">
    <!--
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:advertisement:add']"
        >新增配置
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="inviteList">
      <el-table-column label="最小邀请数量" align="center" prop="minNum" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="最大邀请数量" align="center" prop="maxNum" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="邀请奖励" align="center" prop="rewards" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="邀请用户满足充值金额" align="center" prop="rechargeAsk" width="170" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime, '{m}/{d}/{y}') }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ parseTime(scope.row.updateTime, '{m}/{d}/{y}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['car:advertisement:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['car:advertisement:remove']"
            >删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="最小邀请数" prop="minNum">
          <el-input class="modal-input" v-model="form.minNum" type="number" :min="0" :step="1" placeholder="请输入最小邀请数量"/>
        </el-form-item>
        <el-form-item label="最大邀请数" prop="maxNum">
          <el-input class="modal-input" v-model="form.maxNum" type="number" :min="0" :step="1" placeholder="请输入最大邀请数"/>
        </el-form-item>
        <el-form-item label="邀请奖励" prop="rewards">
          <el-input class="modal-input" v-model="form.rewards" type="number" :min="0" :step="1" placeholder="请输入邀请奖励"/>
        </el-form-item>
        <el-form-item label="邀请用户满足充值金额" prop="rechargeAsk">
          <el-input class="modal-input" v-model="form.rechargeAsk" type="number" :min="0" :step="1" placeholder="请输入邀请用户满足充值金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvite,
  getInvite,
  delInvite,
  addInvite,
  updateInvite
} from '@/api/user/inviteAward'
import Template from '@/views/setting/template/index.vue'

export default {
  name: 'inviteAward',
  computed: {},
  components: { Template },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告表格数据
      inviteList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        minNum: { required: true, message: '最小邀请量不能为空', trigger: 'blur' },
        maxNum: { required: true, message: '最大邀请数量不能为空', trigger: 'blur' },
        rewards: { required: true, message: '邀请奖励不能为空', trigger: 'blur' },
        rechargeAsk: { required: true, message: '邀请用户满足充值金额不能为空', trigger: 'blur' },
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询广告列表 */
    getList() {
      this.loading = true
      listInvite(this.queryParams).then(response => {
        this.inviteList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        minNum:null,
        maxNum:null,
        rewards:null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加配置'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getInvite(row.id).then(res=>{
        this.form = {
          id:res.data.id,
          minNum:res.data.minNum,
          maxNum:res.data.maxNum,
          rewards:res.data.rewards,
          rechargeAsk:res.data.rechargeAsk,
        }
        this.open = true;
        this.title = '修改配置'

      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if(this.form.minNum > this.form.maxNum){
            this.$modal.msgError('最小邀请数不能大于最大邀请数！');
            return;
          }
          if (this.form.id != null){
            updateInvite(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }else {
            addInvite(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })

          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除配置编号为"' + ids + '"的数据项？').then(function() {
        return delInvite( [ids] )
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
